package com.amtf.datafile3.core.xsv.parser;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import com.amtf.datafile3.DataException;
import com.amtf.datafile3.DataFieldParser;
import com.amtf.datafile3.core.std.type.DateField;

public class XsvDateFieldParser extends AbsXsvDataFieldParser<DateField, Date> {
	public XsvDateFieldParser(char quote, boolean trimValue) {
		super(null, quote, trimValue);
	}

	public XsvDateFieldParser(DateFormat format, char quote, boolean trimValue) {
		super(format, quote, trimValue);
	}

	@Override
	protected Date convertString(String value) throws DataException {
		value = value.trim();
		if (value.length() == 0) {
			return null;
		}
		
		try {
			return ((DateFormat) format).parse(value);
		} catch (ParseException e) {
			throw new DataException("Exception when parse date value " + value, e);
		}
	}

	@Override
	public DataFieldParser clone() {
		XsvDateFieldParser newObj = new XsvDateFieldParser((DateFormat) format, quote, trimValue);
		return newObj;
	}
}
